import { defineConfig } from 'vite'

import vue from '@vitejs/plugin-vue'
import vueJsx from '@vitejs/plugin-vue-jsx'
import viteCompression from 'vite-plugin-compression'
import { createSvgIconsPlugin } from 'vite-plugin-svg-icons'
import AutoImport from 'unplugin-auto-import/vite'
import Components from 'unplugin-vue-components/vite'
import { ElementPlusResolver } from 'unplugin-vue-components/resolvers'

import path from 'path'

export default defineConfig(() => {
  return {
    plugins: [
      vue(),
      vueJsx(),
      createSvgIconsPlugin({
        // 指定需要缓存的图标文件夹
        iconDirs: [path.resolve(process.cwd(), 'src/icons')],
        // 指定symbolId格式
        symbolId: 'icon-[dir]-[name]',
      }),
      AutoImport({
        dts: 'src/auto-imports.d.ts',
        imports: ['vue', 'vue-router'],
        // eslint报错解决
        eslintrc: {
          enabled: true, // Default `false`
          filepath: './.eslintrc-auto-import.json', // Default `./.eslintrc-auto-import.json`
          globalsPropValue: true, // Default `true`, (true | false | 'readonly' | 'readable' | 'writable' | 'writeable')
        },
        resolvers: [ElementPlusResolver()],
      }),
      Components({
        dirs: [
          'src/components',
          'src/icons',
          'src/views/**/components/*.{vue|tsx}',
        ],
        extensions: ['vue', 'tsx', 'svg'],
        // 配置文件生成位置
        dts: 'src/components.d.ts',
        resolvers: [
          ElementPlusResolver({
            importStyle: 'sass',
          }),
        ],
      }),
      viteCompression({
        threshold: 1024000, // 对大于 1mb 的文件进行压缩
      }),
    ],
    resolve: {
      alias: {
        '@': path.resolve(__dirname, 'src'),
        '#': path.resolve(__dirname, 'typings'),
        'vue-i18n': 'vue-i18n/dist/vue-i18n.cjs.js',
        '/^~(.*)$/': 'node_modules/$1',
      },
    },
    css: {
      preprocessorOptions: {
        scss: {
          additionalData: `@use "@/styles/element.scss" as *;`,
        },
      },
    },
    define: {
      __VUE_I18N_LEGACY_API__: false,
      __VUE_I18N_FULL_INSTALL__: false,
      __INTLIFY_PROD_DEVTOOLS__: false,
    },
    build: {
      chunkSizeWarningLimit: 1024,
    },
    base: '/web/',
    server: {
      hmr: {
        overlay: false,
      },
      port: 3002,
      open: false,
      https: false,
      proxy: {
        '/admin-core': {
          target: 'http://8.155.170.55:8081',
          changeOrigin: true,
          rewrite: (path) => path.replace(/^\/admin-core/, '/admin-core'),
        },
      },
    },
  }
})
